# Maintainer: Antonio Rojas <arojas@archlinux.org>
# Contributor: Alexander F. Rødseth <xyproto@archlinux.org>
# Contributor: Eli Schwartz <eschwartz@archlinux.org>
# Contributor: Lex Black <autumn-wind@web.de>
# Contributor: Michael Jakl <jakl.michael@gmail.com>
# Contributor: devmotion <nospam-archlinux.org@devmotion.de>
# Contributor: Valentin Churavy <v.churavy@gmail.com>

pkgname=julia
epoch=2
pkgver=1.8.5
pkgrel=5
arch=(loong64 x86_64)
pkgdesc='High-level, high-performance, dynamic programming language'
url='https://julialang.org/'
license=(MIT)
depends=(fftw hicolor-icon-theme libgit2 libunwind libutf8proc openblas64 cblas64 lapack64
         suitesparse64 mbedtls openlibm pcre2 llvm14-libs p7zip libblastrampoline)
makedepends=(cmake gcc-fortran python llvm14 patchelf libwhich)
optdepends=('gnuplot: If using the Gaston Package from julia')
replaces=(julia-docs)
source=(https://github.com/JuliaLang/julia/releases/download/v$pkgver/$pkgname-$pkgver-full.tar.gz{,.asc}
        ed9851b0.patch
        074d761f.patch
        700c69b1.patch
        7b2d1e08.patch
        0db596e6.patch
        190565c2.patch
        68adc00e.patch
        3423639d.patch
        23f39f83.patch
        2ef4aba7.patch
        julia-llvm-15.patch
        LLVM-14.patch
        julia-libunwind-1.6.patch
        julia-system-cblas.patch
        julia-hardcoded-libs.patch
        julia-suitesparse-7.patch
        julia-mpfr-4.2.patch
        https://github.com/JuliaLang/julia/commit/a48ad530.patch
        https://github.com/JuliaLang/julia/commit/0b211609.patch)
backup=(etc/julia/startup.jl)
sha256sums=('35554080a4b4d3ce52ef220254306bd42ac0d88eff9eb85592a57d0663db5df2'
            'SKIP'
            'ce0de27461197ade425cacdd3a642a6b2579e27c7ecc4a457e2b1bde2d76492a'
            '5d4093de84f836e16ec7f83736494e82804ab2fa37bc5594a5b99b1708dce322'
            '8cdc46dd0840eac8ff625c0be867f01f18bd73e7ad4991abb52d46d46daac6a5'
            'f89796cb4c51501fbb5ad667815054f9a4500a9a47fa8dfe497cbe452894e3df'
            '513a48a4a954cbbb7684d2dac1aa287d08dc613b785958406fd9d12ee348f865'
            'aa3b0b03410101bd70293c4012a7bc213afae206dd237172c85477d6125f08a4'
            'c0f504a53be19984fc8dc065057872ad2a98bc5e3801636d0cbdca713fbbf9de'
            '2e736b5a042fb5448ed8622d14f23e9b1c1bb90d003fe511e6d5c45e074780ab'
            '0b2e7262d9de2c8436409696030b01402d5c2dc5996b1a0cfee91c0b025ee0a9'
            'f11bad320938e2f7567890d5c165ecb1a62c1ac6d2c9c27fa058a4d0d142875b'
            '6a3a482ace2d9cba7c4d86443738a0aba47016c6f530ba8d924acb0be1a86548'
            '2de225ce6636a333f0a5f3a7bf61679e02f586c33ad0ab75961b022dfcdc77c1'
            'cea321f7e16381dd795ee20d7bd5eda64f9b453c01d13e960962eb2723907e24'
            '65f24275edb8357ded3c53fd8a10c3d1ed13fa3d34a3869df0c04da10dba6c9d'
            'dd8896da27f691005c3294539fa229823a0ce33802b34287beae8709bbbe535d'
            '8d01fc8f86e15e2ca2e743d559d99cad86f2d301c1b9de0ad01d89aa84684b8e'
            'a47b2e110438082c101392b36d944ee8c40e6113231b88d1ba63b1c1b61b5eaa'
            '6539680cc18af72d6d92be7ce5b5480dd25462888131ad215effed4b6db77d40'
            'dda2e9c99f074f57edbece7b24e8da2600c39b645578ef12d3603bdf528064ab')
validpgpkeys=('3673DF529D9049477F76B37566E3C7DC03D6E495') # Julia (Binary signing key) <buildbot@julialang.org>
options=(!lto)

prepare() {
  cd $pkgname-$pkgver

# Fixes for LLVM 14
  patch -p1 -i ../ed9851b0.patch
  patch -p1 -i ../074d761f.patch
  patch -p1 -i ../700c69b1.patch
  patch -p1 -i ../LLVM-14.patch
# Fixes for LLVM 15 - builds, but many tests fail
#  patch -p1 -i ../0db596e6.patch
#  patch -p1 -i ../7b2d1e08.patch
#  patch -p1 -i ../190565c2.patch
#  patch -p1 -i ../68adc00e.patch
#  patch -p1 -i ../3423639d.patch
#  patch -p1 -i ../23f39f83.patch
#  patch -p1 -i ../2ef4aba7.patch
#  patch -p1 -i ../julia-llvm-15.patch
# libunwind 1.6 compatibility
  patch -p1 -i ../julia-libunwind-1.6.patch
# Add and use option to build with system cblas
  patch -p1 -i ../julia-system-cblas.patch
# Don't hardcode library names
  patch -p1 -i ../julia-hardcoded-libs.patch
# Fix tests with MPFR 4.2
  patch -p1 -i ../julia-mpfr-4.2.patch
# Fix test failures due to using system blas
  sed -e 's|0.22314355f0 + 3.1415927f0im|0.22314355f0 - 3.1415927f0im|' -i stdlib/LinearAlgebra/test/lu.jl
# Find system libstdc++
  patch -p1 -i ../0b211609.patch
# Don't expect libssp.so
  patch -p1 -i ../a48ad530.patch
# Fix warnings with suitesparse 7
  cd stdlib/srccache
  tar -xzf SuiteSparse-f63732c1c6adecb277d8f2981cc8c1883c321bcc.tar.gz
  patch -d JuliaSparse-SuiteSparse.jl-f63732c -p1 < "$srcdir"/julia-suitesparse-7.patch
  rm SuiteSparse-f63732c1c6adecb277d8f2981cc8c1883c321bcc.tar.gz
  tar -czf SuiteSparse-f63732c1c6adecb277d8f2981cc8c1883c321bcc.tar.gz JuliaSparse-SuiteSparse.jl-f63732c
  _suitesparse_md5=$(md5sum SuiteSparse-f63732c1c6adecb277d8f2981cc8c1883c321bcc.tar.gz | cut -d ' ' -f 1)
  _suitesparse_sha512=$(sha512sum SuiteSparse-f63732c1c6adecb277d8f2981cc8c1883c321bcc.tar.gz | cut -d ' ' -f 1)
  sed -e "s|baeb73b8ac38dd04174ed04fa1ea8cef|${_suitesparse_md5}|" \
      -e "s|a95e6ebafe948f419a65a9630b01cda380f3ce19499afe57e212a75dd43aa7a09ddd038e90d1215ae55566a676e392e696565d2d7a96853ec4fca7f73762b268|${_suitesparse_sha512}|" \
      -i ../../deps/checksums/suitesparse
}

_buildopts="prefix=/usr \
    bindir=/usr/bin \
    sysconfdir=/etc \
    libexecdir=/usr/lib \
    USE_BINARYBUILDER=0 \
    USE_SYSTEM_CSL=1 \
    USE_SYSTEM_LLVM=1 \
    USE_SYSTEM_LIBUNWIND=1 \
    USE_SYSTEM_PCRE=1 \
    USE_SYSTEM_BLAS=1 \
    USE_SYSTEM_LAPACK=1 \
    USE_SYSTEM_LIBBLASTRAMPOLINE=1 \
    USE_SYSTEM_GMP=1 \
    USE_SYSTEM_MPFR=1 \
    USE_SYSTEM_LIBSUITESPARSE=1 \
    USE_SYSTEM_LIBWHICH=1 \
    USE_SYSTEM_DSFMT=0 \
    USE_SYSTEM_LIBUV=0 \
    USE_SYSTEM_UTF8PROC=1 \
    USE_SYSTEM_LIBGIT2=1 \
    USE_SYSTEM_LIBSSH2=1 \
    USE_SYSTEM_MBEDTLS=1 \
    USE_SYSTEM_CURL=1 \
    USE_SYSTEM_PATCHELF=1 \
    USE_SYSTEM_ZLIB=1 \
    USE_SYSTEM_P7ZIP=1 \
    USE_SYSTEM_OPENLIBM=1 \
    USE_BLAS64=1 \
    LIBBLAS=-lblas64 \
    LIBBLASNAME=libblas64 \
    LIBCBLAS=-lcblas64 \
    LIBCBLASNAME=libcblas64 \
    LIBLAPACK=-llapack64 \
    LIBLAPACKNAME=liblapack64 \
    LLVM_CONFIG=llvm-config-14 \
    MARCH=x86-64"

build() {
  cd $pkgname-$pkgver
  make release VERBOSE=1 JLDFLAGS=${LDFLAGS} $_buildopts -j1
}

check() {
  cd $pkgname-$pkgver/test
  ln -s /etc/ssl/cert.pem ../usr/share/julia

  # TODO: Remove SuiteSparse from skip list in 1.9 (it is downloaded at build time from a separate repo, not easily patchable)
  ../julia --check-bounds=yes --startup-file=no ./runtests.jl \
    --skip Downloads \
    --skip Sockets \
    --skip nghttp2_jll \
    --skip GMP_jll \
    --skip LibCURL \
    --skip LibSSH2_jll \
    --skip MbedTLS_jll \
    --skip MPFR_jll \
    --skip OpenBLAS_jll \
    --skip SuiteSparse \
    --skip SuiteSparse_jll \
    --skip PCRE2_jll \
    --skip LibGit2_jll \
    --skip Zlib_jll
  find ../stdlib \( -name \*.cov -o -name \*.mem \) -delete
  rm -fr ../stdlib/Artifacts/test/artifacts
}

package() {
  cd $pkgname-$pkgver
  make DESTDIR="$pkgdir" install $_buildopts

  ln -sf /etc/ssl/cert.pem "$pkgdir"/usr/share/julia # Needed by some packages

  rm "$pkgdir"/usr/lib/julia/libccalltest.so.debug # Remove debug testing library
  install -Dm644 LICENSE.md -t "$pkgdir"/usr/share/licenses/$pkgname
}
